import Client from '@/api/client/index.js'
export default {
  namespaced: true,
  state: () => {
    return {
      briefInfo: {},
      permissions: []
    }
  },
  mutations: {
    setBriefInfo(state, info) {
      state.briefInfo = info
    },
    setPermissions(state, permissions) {
      state.permissions = permissions
    }
  },
  actions: {
    async getBriefInfo({ commit }, id) {
      const { data } = await Client.getBrief(id)
      await this.dispatch('dict/getBusinessAreas')
      await this.dispatch('dict/getClientTypes')
      const businessArea = this.state.dict.businessAreas.find(
        businessArea => businessArea.value === data.businessDirection
      )
      const clientType = this.state.dict.clientTypes.find(clientType => clientType.value === data.clientKind)
      data.businessDirectionTransformed = businessArea ? businessArea.text : data.businessDirection
      data.clientKindTransformed = clientType ? clientType.text : data.clientKind
      commit('setBriefInfo', data)
    },
    async getPermissions({ commit, state }, id, forceUpdate = false) {
      if (forceUpdate || state.permissions.length === 0) {
        const { data } = await Client.getPermissions(id)
        commit('setPermissions', data)
      }
    }
  }
}
